package com.yingyi.basic.service.impl;

import com.yingyi.basic.exception.BusinessException;
import com.yingyi.basic.mapper.BaseMapper;
import com.yingyi.basic.query.BaseQuery;
import com.yingyi.basic.service.IBaseService;
import com.yingyi.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * public interface DepartmentMapper extends BaseMapper<Department>
 * public interface EmployeeMapper extends BaseMapper<Employee>
 *
 * @param <T>
 */
//没有添加@Service注解：因为BaseServiceImpl不用交给Spring管理
//且 BaseMapper<T> baseMapper 加了@Autowired注解Spring中但没有相应对象，无法注入
//通过子类继承这些字段， 然后子类使用@Service注解交给Spring管理
//BaseMapper<Department> 对应  DepartmentMapper
//BaseMapper<Employee> 对应  EmployeeMapper


/**
 * Transactional具有继承性
 * @Inherited修饰
 * 子类可以继承该注解，不用再写事务注解，覆写的方法除外
  * @param <T>
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T queryById(Long id) {
        return baseMapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    @Transactional
    public void insert(T t) throws BusinessException {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void update(T t) throws BusinessException {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> queryData(BaseQuery query) {
        //条件查询
        List<T> list = baseMapper.queryData(query);

        //条件查询的结果总数
        Long count = baseMapper.queryCount(query);

        return new PageList<T>(count, list);
    }


    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}
